package com.sfh.base;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.content.SharedPreferencesCompat;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;


/**
 * 功能描述:基类Application
 *
 * @date 2016/12/7
 */

public abstract class AbstractBaseApplication extends Application {

    // 首选项-文件
    private static final String PREF_NAME = "alliance.pref";

    // APP 缓存文件夹名【APP 统一缓存文件夹】
    private static final String CACHE_NAME = "alliance";

    // DeBug 模式
    public abstract boolean isDebug();

    public abstract String getAPIURL();

    // 退出应用重新进入
    public abstract void onLoseToken(String exit);

    //用RxJava实现事件总线
    private final Subject<Object,Object> bus = new SerializedSubject<> (PublishSubject.create ());
    /***
     * 发送一个新的事件
     */
    public void postRxBus (Object event) {
        bus.onNext(event);
    }

    /***
     * 注册【根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 】
     * 对应的注销借口
     * @Link  unRegisterRxBus()
     * @param eventClass
     * @param onNext
     * @param <T>
     * @return
     */
    public <T> Subscription registerRxBus(final Class<T> eventClass, Action1<T> onNext)
    {
        return bus.ofType (eventClass).observeOn(AndroidSchedulers.mainThread()).subscribe (onNext);
    }

    /***
     * 注销
     * @param sub
     */
    public void unRegisterRxBus(Subscription sub) {

        if (sub != null && !sub.isUnsubscribed ()) {
            sub.unsubscribe ();
        }
    }

    
    @Override
    public void onCreate() {

        super.onCreate ();
        new ApplicationEntity.Creator (this).setDebug (isDebug ()).setURL (getAPIURL ()).setCache (CACHE_NAME).build ();
    }


    public static void set(String key, long value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putLong (key, value);
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }
    public static void set(String key, int value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putInt(key, value);
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }
    public static void set(String key, float value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putFloat (key, value);
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }
    public static void set(String key, boolean value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }

    public static void set(String key, String value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(key, value);
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }

    public static boolean get(String key, boolean defValue) {
        return getPreferences().getBoolean(key, defValue);
    }

    public static String get(String key, String defValue) {
        return getPreferences().getString(key, defValue);
    }

    public static int get(String key, int defValue) {
        return getPreferences().getInt(key, defValue);
    }

    public static long get(String key, long defValue) {
        return getPreferences().getLong(key, defValue);
    }

    public static float get(String key, float defValue) {
        return getPreferences().getFloat(key, defValue);
    }

    public static SharedPreferences getPreferences() {
        return ApplicationEntity.getApplication ().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }
}
